#!/usr/bin/python
# --coding:utf-8--

import time

import requests
import re
from bs4 import BeautifulSoup
import csv

headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36',
        'Connection': 'keep-alive',}

url = "https://movie.douban.com/top250?start=75&filter="
print(url)
response = requests.get(url, headers=headers)
# print(response.text)


mov_info = {}
soup = BeautifulSoup(response.text, "lxml")
movies = soup.find_all('div', class_="info")

for info in movies:
    time.sleep(0.1)
    # 获得电影的中文名
    mov_info['mov_name'] = info.find('span', class_='title').text  # find()只找到一个，结果以树结构返回

    # 获得电影在豆瓣中的链接
    mov_info['mov_link'] = info.find('a').get('href')

    # 找到评分以及评价人数
    rating_num = info.find(class_='rating_num')
    print(rating_num)
    mov_info['rating_score'] = rating_num.text
    # print(rating_num.text)


    #
    # comment = rating_num.find_next_sibling().find_next_sibling()
    # # 对评价字段切分
    # comment_num = re.findall('\d{0,}', comment.text)
    # mov_info['comment_nums'] = comment_num[0]  # 正则匹配re中没有find()，findall()以列表形式返回结果
    #
    # # 获得一句话评价
    # comment_one = info.find('span', class_='inq')
    # if comment_one is None:
    #     mov_info['inq_comment'] = u' '
    # else:
    #     mov_info['inq_comment'] = comment_one.text
    # print(mov_info)
